package com.xiaoyu.client.ui.activity.main.mine.collect_and_refundafter;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.mine.ParticularsBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.forum.ForumDetailActivity;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.adapter.AllPowerfulAdapter;
import com.xiaoyu.commonlib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author jx
 * @date 创建时间 2018/10/18
 * @Description 帖子
 */
@SuppressLint("ValidFragment")
public class PostFragment extends Fragment {

    private View mView;
    @BindView(R.id.common_recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.common_recycler_view_empty_page)
    ImageView mEmptyImg;


    private List<CollectBean.DataBean.ListBean> mPostList = new ArrayList<>();
    private AllPowerfulAdapter mPostAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.common_recycler_view, container, false);
            ButterKnife.bind(this, mView);
            initView();
        }
        return mView;
    }

    @Override
    public void onResume() {
        super.onResume();
        getCollectData();
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setBackgroundColor(getResources().getColor(R.color.color_white));
        setPostAdapter(mPostList);

        smartRefreshLayout.setEnableLoadMore(false);
        smartRefreshLayout.setEnableRefresh(false);

    }

    private void setPostAdapter(List<CollectBean.DataBean.ListBean> mPostList) {
        mPostAdapter = new AllPowerfulAdapter<CollectBean.DataBean.ListBean>(R.layout.collect_post_item, mPostList, new AllPowerfulAdapter.OnClickListener<CollectBean.DataBean.ListBean>() {
            @Override
            public void setOnClick(BaseViewHolder baseViewHolder, CollectBean.DataBean.ListBean listBean, int position) {
                //点击跳转详情
                Intent intent = new Intent(getActivity(), ForumDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(ClientConstants.FORUM_ID, listBean.getCollectionid());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        }) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, CollectBean.DataBean.ListBean listBean) {
                super.convert(baseViewHolder, listBean);

                RelativeLayout mRelativeIm = baseViewHolder.getView(R.id.collect_post_item_relative_img);
                LinearLayout mRelativeTxt = baseViewHolder.getView(R.id.collect_post_item_relative_txt);

                if (listBean.getCollectionimg().equals("false")) {
                    mRelativeTxt.setVisibility(View.VISIBLE);
                    mRelativeIm.setVisibility(View.GONE);
                    baseViewHolder.setText(R.id.collect_post_item_explain_txt, listBean.getCollectionname());
                    baseViewHolder.setText(R.id.collect_post_item_time_txt, listBean.getCollectiontime());
                } else {  //有图片
                    mRelativeIm.setVisibility(View.VISIBLE);
                    mRelativeTxt.setVisibility(View.GONE);
                    baseViewHolder.setText(R.id.collect_post_item_explain, listBean.getCollectionname());
                    baseViewHolder.setText(R.id.collect_post_item_time, listBean.getCollectiontime());
                    ImageView mImg = baseViewHolder.getView(R.id.collect_post_item_img);
                    Glide.with(PostFragment.this).load(listBean.getCollectionimg()).into(mImg);
                }
            }
        };
        mRecyclerView.setAdapter(mPostAdapter);
    }

    //获取收藏接口的数据
    private void getCollectData() {
        NetworkManager.getCollectData("t", new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                succeedResult(result);

            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    //解析数据显示内容
    private void succeedResult(String result) {
        CollectBean collectBean = new Gson().fromJson(result, CollectBean.class);
        mPostList.clear();
        mPostList.addAll(collectBean.getData().getList());
        mPostAdapter.notifyDataSetChanged();
        if (collectBean.getData().getList().size() == 0) {
            mEmptyImg.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
            mEmptyImg.setImageResource(R.mipmap.no_data);
        }
    }

}
